package com.hrbxlh.actionImpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.hrbxlh.dao.ChapterDao;
import com.hrbxlh.dao.HomeworkTestDao;
import com.hrbxlh.util.ConfigFileUtil;
import com.hrbxlh.util.FileUpload;

/**
* -------------------------------------------------------
* Copyright (c) 2017, 鑫联华信息科技股份有限公司
* All rights reserved.
* FileName：ChapterActionImpl.java
* Description：章节actionImpl类,负责操作章节数据以及逻辑处理。
* History：
* Date           Author               Desc
* 2017.3.27		  xzx                 建立
* -------------------------------------------------------
*/

@Component("chapterActionImpl")
@Transactional
public class ChapterActionImpl {

	@Resource(name="chapterDao")
	private ChapterDao chapterDao;
	
	@Resource(name="homeworkTestDao")
	private HomeworkTestDao homeworkTestDao;
	
	/**
	 * @description 添加章节
	 * @param Map<String,Object> paramMap
	 * @return boolean (true 成功 ,false失败)
	 * @throws Exception
	 */
	public boolean addChapter(Map<String, Object> paramMap) throws Exception {
		
		boolean result = false;
		try {
			//添加
			chapterDao.addChapter(paramMap);
			result = true;
		} catch (Exception e) {
			result = false;
			throw e;
		}
		return result;
	}
	
	/**
	 * @description 获取某课程的章节信息
	 * @param Map<String,Object> paramMap
	 * @return Map<String,Object>
	 * @throws Exception
	 */
	public List<Map<String, Object>> getCourseChapter(Map<String, Object> paramMap) throws Exception {
		
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			if(paramMap.get("roleId") != null){
				//如果是学生，需要把每个章节是否交了作业以及交作业的评价等
				if(Integer.parseInt(paramMap.get("roleId").toString()) == 1){
					list = chapterDao.getChapterHomeworkList(paramMap);
				//教师正常获取章节	
				}else{
					list = chapterDao.getCourseChapter(paramMap);
				}
			}else{
				list = chapterDao.getCourseChapter(paramMap);
			}
			
			
		} catch (Exception e) {
			throw e;
		}
		return list;
	}
	
	/**
	 * @description 修改章节
	 * @param Map<String,Object> paramMap
	 * @return boolean (true 成功 ,false失败)
	 * @throws Exception
	 */
	public boolean saveChapter(Map<String, Object> paramMap) throws Exception {
		
		boolean result = false;
		try {
			chapterDao.saveChapter(paramMap);
			result = true;
		} catch (Exception e) {
			result = false;
			throw e;
		}
		return result;
	}
	
	/***
	 * @description 修改的时候如果没修改这条文件，就将原来的文件名保存到数据库
	 * @param 文件流
	 * @param 章节id
	 * @param 获取的字段
	 * @param 上传的文件名
	 * @return 文件名
	 */
	public String getFileName(CommonsMultipartFile file, long chapterId,String columnName, String newName, long courseId)throws Exception{
		
		String fileName = "";
		try{
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("chapterId", chapterId);
			map.put("courseId", courseId);
			//没上传要原来的
			if(file.getOriginalFilename().equals("")){
				List<Map<String, Object>> mapResult = chapterDao.getCourseChapter(map);
				if(mapResult != null){
					if(mapResult.size() != 0){
						fileName = (String)mapResult.get(0).get(columnName);
					}
				}
			//上传了文件	
			}else{
				fileName = newName;
			}
		}catch(Exception e){
			throw e;
		}
		return fileName;
	}
	
	/***
	 * @description 修改的时候如果没修改这条文件，就将原来的文件名保存到数据库 ，多个修改
	 * @param 文件流
	 * @param 章节id
	 * @param 获取的字段
	 * @param 上传的文件名
	 * @return 文件名
	 */
	public String[] getFileNameList(CommonsMultipartFile[] files, long[] chapterIds,String[] columnNames, String[] newNames,long courseId)throws Exception{
		
		String[] names = new String[files.length];
		try{
			for(int i = 0; i < files.length; i++){
				String fileName = this.getFileName(files[i], chapterIds[i], columnNames[i], newNames[i],courseId);
				names[i] = fileName;
			}
		}catch(Exception e){
			throw e;
		}
		return names;
	}
	
	/**
	 * @description 查询某课程有没有这个章节
	 * @param courseId,chapterNum
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> getChapterByClassNum(Map<String, Object> paramMap) throws Exception{
		
		Map<String, Object> map = new HashMap<String , Object>();
		try{
			map = chapterDao.getChapterByClassNum(paramMap);
		}catch(Exception e){
			throw e;
		}
		return map;
	}
	
	/**
	 * @description 删除章节信息
	 * @param courseId,chapterNum
	 * @return true成功false失败
	 * @throws Exception
	 */
	public Map<String, Object> delChapter(Map<String, Object> paramMap) throws Exception{
		
		Map<String, Object> result = new HashMap<String, Object>();
		try{
			//查询某个章节的对应的章节全部信息
			Map<String, Object> chapterMsg = chapterDao.getChapterByClassNum(paramMap);
			//查询作业表里面有没有这个课程这个章节的人交作业
			List<Map<String, Object>> taskList = homeworkTestDao.getCourseChapterTask(chapterMsg);
			//如果有作业信息，不能删除
			if(taskList != null && taskList.size() != 0){
				result.put("result", false);
				result.put("failMsg", "学生已经提交作业不可删除");
			}else{
				//删除章节
				chapterDao.delChapter(paramMap);
				Map<String, Object> chapterDetail = chapterDao.getChapterByClassNum(paramMap);
				//删除本章节作业信息
				chapterDao.delChapterTask(chapterDetail);
				//删除本章节学生学习信息
				chapterDao.delChapterStudy(chapterDetail);
				result.put("result", true);
			}
		}catch(Exception e){
			throw e;
		}
		return result;
	}
	
	/**商莹
	 * @description 删除章节中的课件、作业、视频
	 * @param courseId,chapterNum
	 * @return true成功false失败
	 * @throws Exception
	 */
	public Map<String, Object> delChapterCoHoVo(Map<String, Object> paramMap) throws Exception{
		
		Map<String, Object> result = new HashMap<String, Object>();
		FileUpload fileUpload = new FileUpload();
		boolean flag = false;
		try{
			 int type = Integer.valueOf(String.valueOf(paramMap.get("type")));
			//查询某个章节的对应的章节全部信息
			//Map<String, Object> chapterMsg = chapterDao.getChapterByClassNum(paramMap);
			//删除之前上传的文件（如果没有修改，不删除）
			String[] keysArray = {"server_address", "project_name","tomcatUrl"};
			Map<String, Object> pathMap = ConfigFileUtil.getFileMessage("UtilFile.properties", keysArray);
			String path =(String)pathMap.get("tomcatUrl")+(String)pathMap.get("project_name")+"upload/course/";
		    List<Map<String, Object>> chapterList = chapterDao.getCourseChapter(paramMap);
		    if(type==1){
		    	//删除课件
		    	String coursewareName = (String)chapterList.get(0).get("coursewareName");
		    	  flag = fileUpload.deleteFileOnly(path,coursewareName);
		    }if(type==2){
		    	//删除视频
		    	String videoName = (String)chapterList.get(0).get("videoName");
		    	System.out.println("删除的视频"+path+"  "+videoName);
		    	flag = fileUpload.deleteFileOnly(path,videoName);
		    }if(type==3){
		    	//删除作业
		    	String homeworkName = (String)chapterList.get(0).get("homeworkName");
				String homeworkNameDoc = (String) chapterList.get(0).get("homeworkNameDoc");
				System.out.println("删除的是作业"+path+"   "+homeworkNameDoc+"pdf:"+homeworkName);
				flag = fileUpload.deleteFileOnly(path,homeworkName);
				flag = fileUpload.deleteFileOnly(path,homeworkNameDoc);
		    }if(type==5){
		    	//删除备课材料
		    	String courseMaterialName = (String)chapterList.get(0).get("courseMaterialName");
				flag = fileUpload.deleteFileOnly(path,courseMaterialName);
		    }if(type==6){
		    	//删除课后答案
		    	String courseAnswerName = (String)chapterList.get(0).get("courseAnswerName");
				flag = fileUpload.deleteFileOnly(path,courseAnswerName);
		    }
		
			if(flag){
				//删除章节中的课件、作业、视频（某一个）
				flag =	chapterDao.delChapterCoHoVo(paramMap);
			}
			if(flag){
				result.put("result", true);
			}else{
				result.put("result", false);
				result.put("failMsg", "删除失败");
			}
			
			
			
		}catch(Exception e){
			throw e;
		}
		return result;
	}
	
	/**
	 * @description 查询某课程某章节的信息
	 * @param courseId,chapterId
	 * @return Map
	 * @throws Exception
	 */
	public Map<String, Object> getCourseOneChapter(Map<String, Object> paramMap) throws Exception{
		
		Map<String, Object> map = new HashMap<String, Object>();
		try{
			map = chapterDao.getCourseOneChapter(paramMap);
		}catch(Exception e){
			throw e;
		}
		return map;
	}
	
	/**
	 * @description 通过章节名称获取章节信息
	 * @param Map<String,Object> paramMap
	 * @return Map<String,Object>
	 * @throws Exception
	 */
	public List<Map<String, Object>> getChapterByName(Map<String, Object> paramMap) throws Exception {
		
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			list = chapterDao.getChapterByName(paramMap);
		} catch (Exception e) {
			throw e;
		}
		return list;
	}
	
}